// 上传模块

// 调用方法：
// $.post('http://http://api.juzhen05.com/admin/matrixbox/updatetoken'


var path = require('path')
var routerApi = require('../../utils/routeString.js')(__filename, __dirname);
var User = require('./dal/user.js')
var jwt = require('../../utils/jwt.js')
var MartixBox = require('./dal/matrixbox.js')

var checkToken = async function (req, res, next) {
    var token = req.body.token;
    try {
        var tokenData = await jwt.decode(token);
        // 可以使用,并刷新
        var newToken = await jwt.encode(tokenData.userid);
        //  -------- 获取用户信息
        var whereData = {
            id: tokenData.userid
        }
        var user = new User(whereData);
        var results2 = await user.select();
        if (results2.length > 0) {
            next();
            return res.send({ token: newToken, userinfo: results2[0] });
        }
    } catch (error) {
        return res.send({ error_code: 1, msg: "token刷新失败，请重新登陆" });
    }

}


server.post({ path: routerApi, name: routerApi }, checkToken);